package pacs_server

import (
	"errors"
	"fmt"
	"gitee.com/uni-minds/bridge-pacs/dcm4chee"
	"gitee.com/uni-minds/bridge-pacs/global"
	"gitee.com/uni-minds/bridge-pacs/tools"
	"time"
)

func StudiesAnalysis(connector *dcm4chee.Connector, studiesId string) (studiesInfo global.Dcm4cheStudiesInfo, err error) {
	countSeries := connector.CountSeries(studiesId)
	if countSeries < 1 {
		return studiesInfo, errors.New("no studies data")
	}
	jdata, err := connector.GetSeriesData(studiesId, 0, countSeries)
	if err != nil {
		return studiesInfo, err
	}

	studiesMeta, err := connector.GetStudiesMetadata(studiesId)
	if err != nil {
		return studiesInfo, err
	}

	sdate, _ := dcm4chee.QueryDataString(studiesMeta, dcm4chee.DICOM_STUDY_DATE)
	stime, _ := dcm4chee.QueryDataString(studiesMeta, dcm4chee.DICOM_STUDY_TIME)
	sDatetime, _ := tools.Dcm4cheDateTimeDecode(sdate, stime)

	var pn, pid string
	var info global.Dcm4cheSeriesInfo
	seriesInfo := make([]global.Dcm4cheSeriesInfo, 0)

	for _, data := range jdata {
		info, pid, pn, err = SeriesAnalysis(connector, data)
		if err != nil {
			fmt.Println("E", err.Error())
			continue
		}

		seriesInfo = append(seriesInfo, info)

	}

	studiesInfo = global.Dcm4cheStudiesInfo{
		StudiesId:     studiesId,
		URL:           "",
		StudyDatetime: sDatetime.Format(time.RFC3339),
		SeriesInfo:    seriesInfo,
		PatientName:   pn,
		PatientId:     pid,
	}
	return studiesInfo, nil
}

func SeriesAnalysis(connector *dcm4chee.Connector, data map[string]dcm4chee.DicomItem) (seriesInfo global.Dcm4cheSeriesInfo, pid string, pn string, err error) {
	studiesId, _ := dcm4chee.QueryDataString(data, dcm4chee.DICOM_STUDY_ID)
	seriesId, _ := dcm4chee.QueryDataString(data, dcm4chee.DICOM_SERIES_ID)
	seriesNumber, _ := dcm4chee.QueryDataInt(data, dcm4chee.DICOM_SERIES_NUMBER)
	url, _ := dcm4chee.QueryDataString(data, dcm4chee.DICOM_RETRIEVE_URL)
	pn, _ = dcm4chee.QueryDataString(data, dcm4chee.DICOM_PATIENT_NAME)
	pid, _ = dcm4chee.QueryDataString(data, dcm4chee.DICOM_PATIENT_ID)

	sdate, _ := dcm4chee.QueryDataString(data, dcm4chee.DICOM_STUDY_DATE)
	stime, _ := dcm4chee.QueryDataString(data, dcm4chee.DICOM_STUDY_TIME)
	sDatetime, _ := tools.Dcm4cheDateTimeDecode(sdate, stime)

	instanceInfo, err := InstancesAnalysis(connector, studiesId, seriesId)
	if err != nil {
		fmt.Println("E", err.Error())
	} else {
		seriesInfo = global.Dcm4cheSeriesInfo{
			SeriesId:      seriesId,
			SeriesNumber:  seriesNumber,
			URL:           url,
			StudyDatetime: sDatetime.Format(time.RFC3339),
			PatientName:   pn,
			PatientId:     pid,
			InstanceInfo:  instanceInfo,
		}
	}
	return seriesInfo, pid, pn, err
}

func InstancesAnalysis(connector *dcm4chee.Connector, studiesId, seriesId string) (instanceInfo []global.Dcm4cheInstanceInfo, err error) {
	countInstance := connector.CountInstances(studiesId, seriesId)
	if countInstance < 1 {
		return nil, errors.New("no instance data")
	}

	instanceInfo = make([]global.Dcm4cheInstanceInfo, 0)

	jdata, err := connector.GetInstancesData(studiesId, seriesId, 0, countInstance)
	if err != nil {
		return nil, err
	}

	var ctype string
	for _, data := range jdata {

		instanceId, _ := dcm4chee.QueryDataString(data, dcm4chee.DICOM_INSTANCE_ID)
		instanceNumber, _ := dcm4chee.QueryDataInt(data, dcm4chee.DICOM_INSTANCE_NUMBER)
		url, _ := dcm4chee.QueryDataString(data, dcm4chee.DICOM_RETRIEVE_URL)
		cdate, _ := dcm4chee.QueryDataString(data, dcm4chee.DICOM_CONTENT_DATE)
		ctime, _ := dcm4chee.QueryDataString(data, dcm4chee.DICOM_CONTENT_TIME)
		sopClass, _ := dcm4chee.QueryDataString(data, dcm4chee.DICOM_SOP_CLASS)
		height, _ := dcm4chee.QueryDataInt(data, dcm4chee.DICOM_IMAGE_HEIGHT)
		width, _ := dcm4chee.QueryDataInt(data, dcm4chee.DICOM_IMAGE_WIDTH)
		frame, _ := dcm4chee.QueryDataInt(data, dcm4chee.DICOM_MEDIA_MULTI_FRAME)

		cDatetime, _ := tools.Dcm4cheDateTimeDecode(cdate, ctime)

		switch sopClass {
		case dcm4chee.SOP_US_IMAGE, dcm4chee.SOP_SECONDARY_SCREEN:
			ctype = dcm4chee.MEDIA_TYPE_IMAGE
		case dcm4chee.SOP_MULTI_FRAME:
			ctype = dcm4chee.MEDIA_TYPE_MULTI_FRAME
		case dcm4chee.SOP_US_IMAGE_RETIRED:
			ctype = "image_retried"
		case dcm4chee.SOP_COMPREHENSIVE_SR:
			ctype = "comprehensive_sr"
		case dcm4chee.SOP_US_ENHANCE_VOLUME:
			ctype = "us_enhance_volume"
		default:
			fmt.Println("unknown sop:", sopClass)
			ctype = sopClass
		}

		instanceInfo = append(instanceInfo, global.Dcm4cheInstanceInfo{
			InstanceId:      instanceId,
			InstanceNumber:  instanceNumber,
			URL:             url,
			ContentDatetime: cDatetime.Format(time.RFC3339),
			ContentType:     ctype,
			Width:           width,
			Height:          height,
			Frames:          frame,
		})
	}

	return instanceInfo, nil
}
